﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TorqueSensorDriver;

namespace WindowsFormsUI
{
    public partial class MainForm : Form
    {
        private TorqueSensor Sensor;
        private MaGraph Graph;

        public MainForm()
        {
            InitializeComponent();
        }

        private void BtnBegin_Click(object sender, EventArgs e)
        {
            Sensor.SetRatio(10, 300);
            Sensor.Begin(100, 10);
            TmGraph.Start();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Sensor = new TorqueSensor(start_ch: 0)
            {
                QueueLength = 500
            };
            Graph = new MaGraph(ref Zed);
            Graph.SetTitle("传感器数据");
        }

        private void BtnStop_Click(object sender, EventArgs e)
        {
            TmGraph.Stop();
            Sensor.Stop();
        }

        private void TmGraph_Tick(object sender, EventArgs e)
        {
            Sensor.PushData();

            Debug.Print($"Time: {Sensor.GetTimeBuf().Last()}, Master: {Sensor.Master}, Load: {Sensor.Load}");

            Graph.ClearCurve();
            Graph.AddCurve("Master", Sensor.GetTimeBuf(), Sensor.GetMasterBuf(), Color.Blue);
            Graph.AddCurve("Load", Sensor.GetTimeBuf(), Sensor.GetLoadBuf(), Color.Red);
            Graph.RefreshCurve();

            lbCnt0.Text = Sensor.MasterCount.ToString();
            lbCnt1.Text = Sensor.LoadCount.ToString();
        }
    }
}
